<?php

namespace Home\Model;
use Think\Model;
class MobileInfoModel extends BaseModel{
    
    //收件人信息信息表
    protected $tableName = 'tp_mobile_info';

    /**
     * @desc 获取站点下的收件人信息
     * @author quyang
     * @update 2017-02-17
     * @param $user  站点id
     * @param $page  开始页
     * @param $pageNum  分页数量
     * @param string $mobile  收件人手机号
     * @param string $name  收件人姓名
     * @param string $type  收件人状态
     * @return mixed
     */
    public function mobileInfo( $user , $page , $pageNum , $mobile = '' , $name = '' , $type = array() ){

        $where['f_user_id'] = $user;
        if ( $mobile != '' )
        {
            $where['f_recipient_mobile'] = $mobile;
        }
        if ( $name != '' )
        {
            $where['f_recipient_name'] = $name;
        }
        if ( $type != '' )
        {

            $type = $this->returnArray( $type );

            $where['f_status'] = [ 'in' , $type ];
        }

        return $this->where($where)
                ->field('
                    f_id as fId,
                    f_recipient_mobile as fRecipientMobile,
                    f_recipient_name as fRecipientName,
                    f_recipient_phone as fRecipientPhone,
                    f_address as fAddress,
                    f_status as fStatus
                    ')
                ->page( $page , $pageNum )
                ->select();

    }

    /**
     * @desc 统计站点下的收件人信息
     * @author quyang
     * @update 2017-02-17
     */
    public function countMobileInfo( $user , $mobile = '' , $name = '' , $type = array() )
    {

        $where['f_user_id'] = $user;
        if ( $mobile != '' )
        {
            $where['f_recipient_mobile'] = $mobile;
        }
        if ( $name != '' )
        {
            $where['f_recipient_name'] = $name;
        }
        if ( !empty( $type ) )
        {

            $type = $this->returnArray( $type );

            $where['f_status'] = [ 'in' , $type ];
        }

        return $this->where($where)->count('f_id');

    }

    /**
     * @desc  : 根据手机号查询收件人信息
     * @author：quyang
     * @param ：$user：用户id   $mobile：手机号
     * @time  ：2017/2/20 9:57
     * @update：2017/2/20
     **/
    public function queryMobile( $user , $mobile)
    {

        $where['f_user_id'] = $user;
        $where['f_recipient_mobile'] = $mobile;

        return $this->where($where)
            ->field('
                f_id as fId,
                f_recipient_mobile as fRecipientMobile,
                f_recipient_name as fRecipientName,
                f_recipient_phone as fRecipientPhone,
                f_address as fAddress,
                f_status as fStatus
            ')
            ->find();

    }

    /**
     * @Desc  : 更新收件人信息
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/3/22 10:21
     * @Update：2017/3/22
     * @param $fid 收件人id
     * @param $stationId  站点id
     * @param string $fRecipientMobile 收件人手机号
     * @param string $fRecipientName  收件人姓名
     * @param string $fRecipientPhone  收件人联系方式
     * @param string $address  收件人地址
     * @param string $status  收件人状态 (0.正常 -1.加入黑名单 1.删除)
     * @return bool
     */
    public function updateMobile( $fid , $stationId , $fRecipientMobile = '' , $fRecipientName = '' , $fRecipientPhone = '' , $address = '' , $status = '' )
    {

        $where = array(
            'f_id'      =>  $fid,
            'f_user_id' =>  $stationId
        );

        if ( $fRecipientMobile != '' )
        {
            $update_data['f_recipient_mobile'] = $fRecipientMobile;
        }
        if ( $fRecipientName != '' )
        {
            $update_data['f_recipient_name'] = $fRecipientName;
        }
        if ( $fRecipientPhone != '' )
        {
            $update_data['f_recipient_phone'] = $fRecipientPhone;
        }
        if ( $address != '' )
        {
            $update_data['f_address'] = $address;
        }
        if ( $status != '' )
        {
            $update_data['f_status'] = intval( $status );
        }

        return $this->where( $where )->save( $update_data );

    }

    /**
     * @Desc  : 根据手机号查看收件人状态
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/3/23 10:30
     * @Update：2017/3/23
     * @param $stationId 站点id
     * @param $mobile  收件人手机号
     * @param string $field  查询条件
     * @param string $status  状态
     * @return mixed
     */
    public function mobileStatus( $stationId , $mobile , $status = '' ,  $field = '' )
    {

        if ( $field == '' )
        {
            $field = '*';
        }

        $where['f_user_id'] = $stationId;
        $where['f_recipient_mobile'] = array( 'in' , $mobile );

        if ( $status != '' )
        {
            $where['f_status'] = $status;
        }

        return $this->where( $where )->field( $field )->select();

    }

    /**
     * @Desc  : 新增联系人
     * @Author：quyang
     * @Param ：$stationId  站点id  $fRecipientMobile  收件人手机号  $fRecipientName  收件人  $fRecipientPhone收件人电话  $address详细地址  $status黑名单状态
     * @Time  ：2017/3/27 11:04
     * @Update：2017/3/27
     **/
    public function addMobile( $stationId , $fRecipientMobile , $fRecipientName = '' , $fRecipientPhone = '' , $address = '' , $status = '' )
    {

        $add_data['f_user_id'] = $stationId;

        $add_data['f_recipient_mobile'] = $fRecipientMobile;
        $add_data['f_create_time'] = time();

        if ( $fRecipientName != '' )
        {
            $add_data['f_recipient_name'] = $fRecipientName;
        }
        if ( $fRecipientPhone != '' )
        {
            $add_data['f_recipient_phone'] = $fRecipientPhone;
        }
        if ( $address != '' )
        {
            $add_data['f_address'] = $address;
        }
        if ( $status != '' )
        {
            $add_data['f_status'] = intval( $status );
        }

        return $this->add( $add_data );

    }

    /**
     * @Desc  : 查询收件人
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/3/30 13:43
     * @Update：2017/3/30
     * @param $stationId 站点id
     * @param int $recipientMobile 收件人手机号
     * @param int $fId 收件人id
     * @return mixed
     */
    public function queryRecipientMobile( $stationId , $recipientMobile = 0 , $fId = 0 , $status = '' )
    {

        $where = "`f_user_id` = {$stationId}";

        if ( $recipientMobile != 0 )
        {
            $where .= " AND `f_recipient_mobile` = {$recipientMobile}";
        }

        if ( $fId != 0 )
        {
            $where .= " AND `f_id` = {$fId}";
        }

        if ( $status != '' )
        {
            $where .= " AND `f_status` = {$status}";
        }

        return $this->where( $where )->find();

    }

    /**
     * @Desc  : 查询收件人默认通知方式
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/4/26 11:36
     * @Update：2017/4/26
     **/
    public function querySendLabelList( $stationId , $mobile )
    {

        $where = "
            m.`f_user_id` = {$stationId} AND m.`f_recipient_mobile` = {$mobile}
        ";

        return $this->alias('m')
                    ->where( $where )
                    ->field( 'r.f_id,r.f_receive_label_id,r.f_type' )
                    ->join( 'left join tp_receive_label r ON m.f_id = r.f_receive_id' )
                    ->find();

    }

    /**
     * @Desc  : 查询收件人默认通知方式
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/4/26 11:36
     * @Update：2017/4/26
     **/
    public function querySendLabel( $stationId , $mobile )
    {

        $where = "
            m.`f_user_id` = {$stationId} AND m.`f_recipient_mobile` = {$mobile} AND r.`f_receive_label_id` in (101,102,103)
        ";

        return $this->alias('m')
            ->where( $where )
            ->field( 'r.f_id,r.f_receive_label_id,r.f_type' )
            ->join( 'left join tp_receive_label r ON m.f_id = r.f_receive_id' )
            ->find();

    }

    /**
     * @Desc  : 方法名
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/5/6 14:35
     * @Update：2017/5/6
     **/
    public function returnArray( $string )
    {

        if ( is_array( $string ) )
        {
            return $string;
        }
        else
        {
            return array( $string );
        }

    }

}